'''
Created on 2011-11-21

@author: Administrator
'''
import urllib2
import datetime
class IBStock():
    #reg=['AMEX'NASDAQ,NYSE
    def getstocklist(self,reg):
        def getonepagelist(snum):
            url='http://www.interactivebrokers.com.hk/en/trading/exchanges.php?exch='+reg+'&showcategories=STK&showproducts=&sequence_idx='+snum
#            print url
            req = urllib2.Request(url)
            req.add_header('Host', 'www.interactivebrokers.com.hk')
            req.add_header('User-Agent', 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:8.0.1) Gecko/20100101 Firefox/8.0.1')
            req.add_header('Accept-Language', 'zh-cn,zh;q=0.5')
            req.add_header('Accept-Encoding', 'gzip, deflate')
            req.add_header('Accept-Charset', 'GB2312,utf-8;q=0.7,*;q=0.7')
            req.add_header('Accept', 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8')

            try:
                content=urllib2.urlopen(req).read()
            except urllib2.HTTPError , http_err:
#                print 'http error'
                content=http_err.read()
#                print content
            except Exception , err:
                print "Unknown Error:" + str(err)
                return []
#            print len(content)
            ps=content.find('<td class="CellTitle" colspan="4">Stocks</td>')
#            print ps
            if ps<0:return []
            pe=content.find('</table',ps)
            slist=content[ps:pe].split('<tr')
            slist.pop(0)
            slist.pop(0)
            slist.pop(0)
#            print len(slist)

            stocklist=[]
            for s in slist:
                klist=s.split('</td>')
                i=klist[2].find('>')
                stocklist.append(klist[2][i+1:])
            return stocklist
    ####################################
#        print reg
        slist={}
        snum=100
        tlist=getonepagelist(str(snum))
#        print len(tlist),
        for t in tlist:
            if t.find(' ')<0: slist[t]=t
        while len(tlist)==100:
            snum=snum+100
            tlist=getonepagelist(str(snum))
#            print len(tlist),
            for t in tlist:
                if t.find(' ')<0: slist[t]=t
        return slist

if __name__ == '__main__':
    #amex,nasdaq,nyse
    starttime=datetime.datetime.now()
    bb=IBStock()    
    result=bb.getstocklist('nyse')
    print len(result)
    print 'NSAAR' in result.keys()
#    for r in result.keys():
#        print r
    
    print starttime,(datetime.datetime.now()-starttime).seconds
    pass